package com.design.patterns.list.state.eg;

import java.util.Random;

/**
 * 能抽奖
 *
 * @author Wangzhiwen
 */
public class CanState implements ActivityState {

    private final ActivityContext context;

    public CanState(ActivityContext context) {
        this.context = context;
    }

    @Override
    public void deduct() {
        System.out.println("当前状态已扣除过积分了");
    }

    @Override
    public boolean lottery() {
        System.out.println("正在抽奖");
        Random r = new Random();
        int num = r.nextInt(10);
        // 10%中奖机会
        if (num == 0) {
            // 改变活动状态为发放奖品 context
            context.setState(context.dispenseState);
            return true;
        } else {
            System.out.println("很遗憾没有抽中奖品！");
            // 改变状态为不能抽奖
            context.setState(context.noState);
            return false;
        }
    }

    @Override
    public void grant() {
        System.out.println("当前状态能发放奖品");
    }
}
